package com.century.sjt.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.entity.AboutSJT;
import com.century.sjt.util.Constant;
import com.century.sjt.util.QRBitmapUtil;
import com.century.sjt.util.TipUtil;
import com.google.zxing.WriterException;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * @author:周玲波
 * @Description: 我的邀请码
 * @date : 2016/8/16 10:11
 */
public class InNumberFragment extends BaseFragment{
    private Context aContext;
    private String inviteCode,inviteUrl;
    private ImageView inImage;
    private Button btn_Into;
    protected int mScreenWidth ;
    private TextView showNumber;
    private String shareContent=null;
    private String sharethemePic1=null;

    private RequestQueue mQueue;
    private AboutSJT mAboutSJT;
    private LinearLayout intoLayout;
    private String LogoURL;
    private String inviteTitle;
    private String inviteContent;
    private String mtransLanguageName;
    SharedPreferences mainSharedPreferences;
    private String userId;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.sjt_innumber_fragment,null);
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        aContext=getActivity();
        mQueue = Volley.newRequestQueue(aContext);
        initViews();
        Create2QR();


    }

    private void initViews() {
        btn_Into=(Button)getView().findViewById(R.id.btn_set_into);
        showNumber=(TextView)getView().findViewById(R.id.in_show_number);
        inImage= (ImageView) getView().findViewById(R.id.minnumberimage);
        intoLayout = (LinearLayout)getView().findViewById(R.id.sjt_into_layout);
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        mScreenWidth = dm.widthPixels;
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
        inviteCode = sharedPreferences.getString("inviteCode", "");
        inviteUrl =sharedPreferences.getString("inviteUrl","");

        if (inviteCode.equals("0")){
            intoLayout.setVisibility(View.GONE);
            showNumber.setText(getResources().getString(R.string.error_isno_invitation_authority));
        }else {
            showNumber.setText(inviteCode);
        }

        btn_Into.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAboutSJT();
            }
        });
    }
    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    //生成二维码
    public void Create2QR(){

        String uri = inviteUrl;
        Bitmap bitmap;
        try {
            bitmap = QRBitmapUtil.createQRCode(uri, mScreenWidth);

            if(bitmap != null){
                inImage.setImageBitmap(bitmap);

            }

        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

    }



    public void showShare(String LogoURL,String inviteTitle, String inviteContent) {
//图片不能为空，地址要有图片
//        mainSharedPreferences = getActivity().getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
//        userId = mainSharedPreferences.getString("userId", "");
//        inviteTitle = mainSharedPreferences.getString("isTrans","");
//        inviteContent = mainSharedPreferences.getString("transLanguage","");
        ShareSDK.initSDK(getActivity());
        OnekeyShare oks = new OnekeyShare();
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitleUrl(inviteUrl);
        oks.setTitle(inviteTitle);
        oks.setImageUrl(Constant.BaseImageUrl+LogoURL);
        oks.setText(inviteContent);
        oks.setUrl(inviteUrl);
        oks.show(getActivity());

    }

    /**
     * 获取关于世纪通的信息
     */
    private void getAboutSJT() {
        //显示Progress Dialog
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.GetAboutSJT, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {

                    TipUtil.log("接口成功返回", response.toString(), 1);
                    JSONTokener jsonParser = new JSONTokener(response);
                    JSONObject res = (JSONObject) jsonParser.nextValue();
                    String code = res.getString("code");
                    String message = res.getString("message");
                    JSONObject Data=res.getJSONObject("data");
                    if (code.equals("407")){
                        TipUtil.showAppayRes(getActivity());
                    }
                    if (code.equals("200")) {//成功
                        LogoURL=Data.getString("logo");
                        inviteContent = Data.getString("inviteContent");
                        inviteTitle = Data.getString("inviteTitle");
                        showShare(LogoURL,inviteTitle,inviteContent);
                    } else {//失败
                        TipUtil.showToast(message, aContext, 1);
                    }
                } catch (JSONException ex) {
                    ex.printStackTrace();
                    TipUtil.showToast(getResources().getString(R.string.error_service), aContext, 1);
                }

                TipUtil.closeProgressDialog();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                TipUtil.showToast(getResources().getString(R.string.error_network), aContext, 1);
                TipUtil.closeProgressDialog();
            }
        });
        mQueue.add(stringRequest);
    }



}
